<?php

	/*
	 * AJAX call to load the list of SMS messages.
	 * Uses variables from settings database table, as well as filters (content and phonenumber).
	 *
	 * Variables:
	 *   id (int) : Record ID of the last already shown SMS
	 *
	 * Returns: xml
	 *
	 */


	header('Content-type: text/xml');


	$begindate = $settings["party_date"]." ".$settings["party_hour"].":00";	// Only messages with datetime > $begindate are used (except for random, see $random_also_old)

	$lastd = preg_replace("/\D/","",$begindate);

	$filters_words = array();
	if($settings["ban_by_content"] == 1){
		$sql = mysql_query("SELECT * FROM filterwords ORDER BY fwText", $db);
		while($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
			array_push($filters_words, $row["fwText"]);
		}
	}

	$filters_numers = array();
	if($settings["ban_by_number"] == 1){
		$sql = mysql_query("SELECT * FROM filternumbers ORDER BY fnNR", $db);
		while($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
			array_push($filters_numers, $row["fnNR"]);
			array_push($filters_numers, "+".$row["fnNR"]);
		}
	}

	$clean["id"] = round($_GET["id"]);
	$clean["limit"] = round($_GET["lim"]);

	if($_GET["dir"] == "up"){
		// Get a certain ID and higher, excluding the ID
		$smssql = mysql_query("SELECT * FROM inbox WHERE ID>".$clean["id"]." AND UpdatedInDB > $lastd ".($settings["ban_by_number"]?" AND SenderNumber NOT IN (\"".implode("\",\"", $filters_numers)."\") ":"").($settings["ban_by_content"]?" AND TextDecoded NOT LIKE \"%".implode("%\" AND TextDecoded NOT LIKE \"%", $filters_words)."%\"":"")." ORDER BY ID DESC");
		if($clean["limit"] > 0){
			$temp = mysql_num_rows($smssql);
			$smssql = mysql_query("SELECT * FROM inbox WHERE ID>".$clean["id"]." AND UpdatedInDB > $lastd ".($settings["ban_by_number"]?" AND SenderNumber NOT IN (\"".implode("\",\"", $filters_numers)."\") ":"").($settings["ban_by_content"]?" AND TextDecoded NOT LIKE \"%".implode("%\" AND TextDecoded NOT LIKE \"%", $filters_words)."%\"":"")." ORDER BY ID DESC LIMIT ".max(0, ($temp-$clean["limit"])).", ".$clean["limit"]);
		}
	}else{
		// Get a certain ID and lower, excluding the ID
		if($clean["id"] > 0){
			$smssql = mysql_query("SELECT * FROM inbox WHERE ID<".$clean["id"]." AND UpdatedInDB > $lastd ".($settings["ban_by_number"]?" AND SenderNumber NOT IN (\"".implode("\",\"", $filters_numers)."\") ":"").($settings["ban_by_content"]?" AND TextDecoded NOT LIKE \"%".implode("%\" AND TextDecoded NOT LIKE \"%", $filters_words)."%\"":"")." ORDER BY ID DESC".(($clean["limit"] > 0)?' LIMIT '.$clean["limit"]:''));
		}else{
			$smssql = mysql_query("SELECT * FROM inbox WHERE UpdatedInDB > $lastd ".($settings["ban_by_number"]?" AND SenderNumber NOT IN (\"".implode("\",\"", $filters_numers)."\") ":"").($settings["ban_by_content"]?" AND TextDecoded NOT LIKE \"%".implode("%\" AND TextDecoded NOT LIKE \"%", $filters_words)."%\"":"")." ORDER BY ID DESC".(($clean["limit"] > 0)?' LIMIT '.$clean["limit"]:''));
		}
	}



	echo '<root>';
	$sms = array();
	while($row = mysql_fetch_array($smssql, MYSQL_ASSOC)){
		array_push($sms, $row);
	}
	if(($_GET["dir"] == "up") or ($clean["id"] == 0)){
		$sms = array_reverse($sms);
	}
	foreach($sms as $row){
		echo "\n".'	<sms time="'.substr($row["ReceivingDateTime"], 5, 11).'" id="'.$row["ID"].'" from="'.$row["SenderNumber"].'"><![CDATA['.($row["TextDecoded"]).']]></sms>';
	}
	echo "\n".'</root>';

?>